//
//  LZMyCollectionView.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/31.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyCollectionView.h"
#import "LZMyCollectionViewCell.h"

#import "Masonry.h"

#import "LZUserModel.h"
#import "LZCurrentUserInfo.h"

@interface LZMyCollectionView () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) LZMyHeadView *headerView;
@end

@implementation LZMyCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {

    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        
        self.alwaysBounceVertical = YES;
        
        [self setup];
    
    }
    return self;
}

- (void)setup {

    self.delegate = self;
    self.dataSource = self;
    self.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    self.showsVerticalScrollIndicator = NO;
    
    [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"white_CollectionCell"];
    [self registerClass:[LZMyCollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    [self registerClass:[LZMyHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
}

- (void)updateHeaderView {
    [self.headerView reloadSubview];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    /* if (indexPath.row == self.titleArray.count) {
        static NSString *identify = @"white_CollectionCell";
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        
        return cell;
    }
    else {
        static NSString *identify = @"CollectionCell";
        LZMyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
        cell.titleLabel.text = self.titleArray[indexPath.row];
        cell.iconImageView.image = [UIImage imageNamed:self.iconArray[indexPath.row]];
        cell.indexPath = indexPath;
        if (indexPath.row == 1) {
            [cell showRedPointView:self.messageHaveUnRead];
        }else{
            [cell showRedPointView:NO];
        }
        
        return cell;
    }
     */
    static NSString *identify = @"CollectionCell";
    LZMyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    cell.titleLabel.text = self.titleArray[indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:self.iconArray[indexPath.row]];
    cell.indexPath = indexPath;
    if (indexPath.row == 1) {
        [cell showRedPointView:self.messageHaveUnRead];
    }else{
        [cell showRedPointView:NO];
    }
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        
        static NSString * Identifier = @"HeaderView";
        self.headerView = (LZMyHeadView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Identifier forIndexPath:indexPath];
        
        WS(weakSelf);
        
        self.headerView.myHeadViewButtonActionBlock = ^(LZ_MY_HEAD_BUTTON_TAG buttonTag) {
            
            if (weakSelf.myHeadViewButtonActionBlock) {
                weakSelf.myHeadViewButtonActionBlock(buttonTag);
            }
        };
        
        return self.headerView;
    }
    
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.titleArray.count) {
        return;
    }
    if (self.didSelectRowBlock) {
        self.didSelectRowBlock(indexPath);
    }
}

@end
